package org.budo.phone.api;

import java.util.Map;

import org.budo.support.lang.util.MapUtil;

import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;

/**
 * @author limingwei
 */
@Getter
@Setter
@Accessors(chain = true)
@Slf4j
public class AbstractPhoneEvent implements BudoPhoneEvent {
    private Type type;

    private CallState callState;

    private String callId;

    private String aLegCallId;

    private String sipUser;

    private String customerNumber;

    public String postNumber;

    private String merchantNumber;

    private String toNumber;

    private String fromNumber;

    private Direction direction;

    private Map<String, String> headers;

    public void setHeaders(Map<String, String> headers) {
        if (null != this.headers) {
            this.headers.putAll(headers);
            return;
        }

        this.headers = headers;
    }

    public String getHeader(String key) {
        return MapUtil.get(this.getHeaders(), key, null);
    }

    public void setHeader(String key, String value) {
        if (null != this.headers) {
            this.headers.put(key, value);
            return;
        }

        this.headers = MapUtil.stringMap(key, value);
    }

    @Override
    public String getFromNumber() {
        if (null != this.fromNumber) {
            return this.fromNumber;
        }

        if (Direction.Outbound.equals(this.getDirection())) {
            return this.merchantNumber;
        }

        if (Direction.Inbound.equals(this.getDirection())) {
            return this.customerNumber;
        }

        log.error("#76 this=" + this);
        return this.fromNumber + "-" + this.toNumber;
    }

    @Override
    public String getToNumber() {
        if (null != this.toNumber) {
            return this.toNumber;
        }

        if (Direction.Outbound.equals(this.getDirection())) {
            return this.customerNumber;
        }

        if (Direction.Inbound.equals(this.getDirection())) {
            return this.merchantNumber;
        }

        log.error("#93 this=" + this);
        return this.fromNumber + "-" + this.toNumber;
    }

    @Override
    public String getCustomerNumber() {
        if (null != this.customerNumber) {
            return this.customerNumber;
        }

        if (Direction.Outbound.equals(this.getDirection())) {
            return this.toNumber;
        }

        if (Direction.Inbound.equals(this.getDirection())) {
            return this.fromNumber;
        }

        log.error("#110 this=" + this);
        return this.fromNumber + "-" + this.toNumber;
    }

    @Override
    public String getMerchantNumber() {
        if (null != this.merchantNumber) {
            return this.merchantNumber;
        }

        if (Direction.Outbound.equals(this.getDirection())) {
            return this.fromNumber;
        }

        if (Direction.Inbound.equals(this.getDirection())) {
            return this.toNumber;
        }

        log.error("#128 this=" + this);
        return this.fromNumber + "-" + this.toNumber;
    }

    @Override
    public String toString() {
        return "AbstractPhoneEvent [type=" + type //
                + ", getCallState=" + callState //
                + ", callId=" + callId //
                + ", sipUser=" + sipUser //
                + ", customerNumber=" + customerNumber //
                + ", postNumber=" + postNumber //
                + ", merchantNumber=" + merchantNumber //
                + ", toNumber=" + toNumber //
                + ", fromNumber=" + fromNumber //
                + ", direction=" + direction + "]";
    }
}